home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / dme / subs.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  7KB  |  422 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6.  
  7. /*
  8.  *  SUBS.C
  9.  *
  10.  *  Subroutines.
  11.  */
  12.  
  13. #include "defs.h"
  14.  
  15.  
  16. Prototype void makemygadget (struct Gadget *);
  17. Prototype int firstns (char *);
  18. Prototype int lastns (char *);
  19. Prototype int wordlen (char *);
  20. Prototype int getpathto (BPTR, char *, char *);
  21. Prototype void *allocb (int);
  22. Prototype void *allocl (int);
  23. Prototype void bmovl (void *, void *, long);
  24. Prototype int detab (char *, char *, int);
  25. Prototype int xefgets (FILE *, char *, int);
  26. Prototype int ncstrcmp (unsigned char *, unsigned char *);
  27. Prototype struct _ED *finded (char *, int);
  28. Prototype void mountrequest(int);
  29. Prototype int DeadKeyConvert(struct IntuiMessage *, UBYTE *, int, struct KeyMap *);
  30. Prototype FONT * GetFont(char *, short);
  31. Prototype char *GetDEnv(char *);
  32. Prototype void SetDEnv(char *, char *);
  33.  
  34. Prototype extern __stkargs int arpreq (char *, char *, char *, WIN *);
  35.  
  36. Prototype extern void *clrmem(void *, size_t);
  37.  
  38.  
  39. typedef struct FileInfoBlock FIB;
  40. typedef struct Process         PROC;
  41.  
  42.  
  43. /*
  44.  *  Create DME's text icon.
  45.  */
  46.  
  47. void
  48. makemygadget(gad)
  49. struct Gadget *gad;
  50. {
  51.     static unsigned long ga[] = {
  52.     0xFFFFFFFF,    /* 32 pixels across */
  53.     0x80FDCBFD,
  54.     0xFFFDDFFD,
  55.     0x80000001,
  56.     0x80DFDDDF,
  57.     0x80000001,
  58.     0xBC0EF00B,
  59.     0x80000001,
  60.     0xBFC00CDD,
  61.     0x80000001,
  62.     0xA00DF00F,
  63.     0x80000001,
  64.     0x80000001,
  65.  
  66.     0x80000001,
  67.     0x80FDCBFD,
  68.     0xFFFDDFFD,
  69.     0x80000001,
  70.     0x80DFDDDF,
  71.     0x80000001,
  72.     0xBC0EF00B,
  73.     0x80000001,
  74.     0xBFC00CDD,
  75.     0x80000001,
  76.     0xA00DF00F,
  77.     0x80000001,
  78.     0xFFFFFFFF
  79.     };
  80.     static struct Image image = {
  81.     0, 0, 20, 16, 2, (unsigned short *)ga, 3, 0, NULL
  82.     };
  83.     clrmem(gad, sizeof(struct Gadget));
  84.     gad->Width = 20;
  85.     gad->Height = 17;
  86.     gad->Flags    = GADGIMAGE|GADGHCOMP;
  87.     gad->GadgetType   = BOOLGADGET;
  88.     gad->Activation = RELVERIFY|GADGIMMEDIATE;
  89.     gad->GadgetRender = (APTR)ℑ
  90. }
  91.  
  92. /*
  93.  * return index of first non space.  Returns 0 if no spaces found.
  94.  */
  95.  
  96. firstns(str)
  97. char *str;
  98. {
  99.     short i;
  100.  
  101.     for (i = 0; str[i] && str[i] == ' '; ++i);
  102.     if (str[i] == 0)
  103.     i = 0;
  104.     return((int)i);
  105. }
  106.  
  107. /*
  108.  *  Return index of last non-space, 0 if no spaces.
  109.  */
  110.  
  111. lastns(str)
  112. char *str;
  113. {
  114.     short i;
  115.  
  116.     for (i = strlen(str) - 1; i > 0 && str[i] == ' '; --i);
  117.     if (i < 0)
  118.     i = 0;
  119.     return((int)i);
  120. }
  121.  
  122. /*
  123.  *  Return length of word under cursor
  124.  */
  125.  
  126. wordlen(str)
  127. char *str;
  128. {
  129.     short i;
  130.  
  131.     for (i = 0; *str && *str != ' '; ++i, ++str);
  132.     return((int)i);
  133. }
  134.  
  135. /*
  136.  *  Backtracks the program lock, 0 on failure, 1 on success.
  137.  */
  138.  
  139. getpathto(lock, arg0, buf)
  140. BPTR lock;
  141. char *arg0;
  142. char *buf;
  143. {
  144.     FIB *fib;
  145.     BPTR parLock = NULL;
  146.     int r = 0;
  147.     short lastDir = 0;
  148.  
  149.     buf[0] = 0;
  150.     if (fib = malloc(sizeof(FIB))) {
  151.     r = 1;
  152.     while (lock) {
  153.         fib->fib_FileName[0] = 0;
  154.         if (Examine(lock, fib) == 0)
  155.         r = 0;
  156.         if (parLock) {
  157.         parLock = ParentDir(lock);
  158.         UnLock(lock);
  159.         } else {
  160.         parLock = ParentDir(lock);
  161.         if (fib->fib_DirEntryType > 0)
  162.             lastDir = 1;
  163.         }
  164.         if (parLock == NULL)
  165.         strins(buf, ":");
  166.         strins(buf, fib->fib_FileName);
  167.         lock = parLock;
  168.     }
  169.     free(fib);
  170.     if (lastDir == 1) {
  171.         short len;
  172.         if ((len = strlen(buf)) && buf[len-1] != ':')
  173.         strcat(buf, "/");
  174.         strcat(buf, arg0);
  175.     }
  176.     }
  177.     return(r);
  178. }
  179.  
  180. /*
  181.  *  Allocation routines and other shortcuts
  182.  */
  183.  
  184. void *
  185. allocb(bytes)
  186. {
  187.     return(AllocMem(bytes, MEMF_CLEAR|MEMF_PUBLIC));
  188. }
  189.  
  190. void *
  191. allocl(lwords)
  192. {
  193.     return(AllocMem(lwords<<2, MEMF_CLEAR|MEMF_PUBLIC));
  194. }
  195.  
  196. void
  197. bmovl(s,d,n)
  198. void *s, *d;
  199. long n;
  200. {
  201.     movmem(s, d, n << 2);
  202. }
  203.  
  204. /*
  205.  *  Remove tabs in a buffer
  206.  */
  207.  
  208. detab(ibuf, obuf, maxlen)
  209. char *ibuf, *obuf;
  210. {
  211.     short i, j;
  212.  
  213.     maxlen -= 2;
  214.     for (i = j = 0; ibuf[i] && j < maxlen; ++i) {
  215.     if (ibuf[i] == 9) {
  216.         do {
  217.         obuf[j++] = ' ';
  218.         } while ((j & 7) && j < maxlen);
  219.     } else {
  220.         obuf[j++] = ibuf[i];
  221.     }
  222.     }
  223.     if (j && obuf[j-1] == '\n')
  224.     --j;
  225.     while (j && obuf[j-1] == ' ')
  226.     --j;
  227.     obuf[j] = 0;
  228.     return((int)j);
  229. }
  230.  
  231. xefgets(fi, buf, max)
  232. FILE *fi;
  233. char *buf;
  234. int max;
  235. {
  236.     char ebuf[256];
  237.  
  238.     if (fgets(ebuf, max, fi))
  239.     return(detab(ebuf, buf, max));
  240.     return(-1);
  241. }
  242.  
  243. ncstrcmp(s1, s2)
  244. ubyte *s1, *s2;
  245. {
  246.     ubyte c1, c2;
  247.  
  248.     for (;;) {
  249.     c1 = *s1;
  250.     c2 = *s2;
  251.     if (c1 >= 'A' && c1 <= 'Z') c1 |= 0x20;
  252.     if (c2 >= 'A' && c2 <= 'Z') c2 |= 0x20;
  253.     if (c1 != c2)
  254.         break;
  255.     if ((c1|c2) == 0)
  256.         return(0);
  257.     ++s1;
  258.     ++s2;
  259.     }
  260.     if (c1 < c2)
  261.     return(-1);
  262.     if (c1 > c2)
  263.     return(1);
  264. }
  265.  
  266. ED *
  267. finded(str, doff)
  268. char *str;
  269. {
  270.     ED *ed;
  271.  
  272.     for (ed = (ED *)DBase.mlh_Head; ed->Node.mln_Succ; ed = (ED *)ed->Node.mln_Succ) {
  273.     if (strlen(ed->Name) >= doff && ncstrcmp(str, ed->Name+doff) == 0)
  274.         return(ed);
  275.     }
  276.     return(NULL);
  277. }
  278.  
  279. void
  280. mountrequest(bool)
  281. int bool;
  282. {
  283.     static APTR original_pr_WindowPtr = NULL;
  284.     register PROC *proc;
  285.  
  286.     proc = (PROC *)FindTask(0);
  287.     if (!bool && proc->pr_WindowPtr != (APTR)-1) {
  288.     original_pr_WindowPtr = proc->pr_WindowPtr;
  289.     proc->pr_WindowPtr = (APTR)-1;
  290.     }
  291.     if (bool && proc->pr_WindowPtr == (APTR)-1)
  292.     proc->pr_WindowPtr = original_pr_WindowPtr;
  293. }
  294.  
  295. char *
  296. GetDEnv(ename)
  297. char *ename;
  298. {
  299.     char *str;
  300.  
  301.     if (str = getenv(ename)) {
  302.     str = strdup(str);
  303.     }
  304.     return(str);
  305. }
  306.  
  307. #ifdef NOTDEF /* old code */
  308.  
  309.     long envLock = Lock("env:", SHARED_LOCK);
  310.     char *str = NULL;
  311.  
  312.     if (envLock) {
  313.     long oldLock = CurrentDir(envLock);
  314.     FILE *fi = fopen(ename, "r");
  315.     long siz;
  316.     if (fi) {
  317.         fseek(fi, 0L, 2);
  318.         siz = ftell(fi);
  319.         fseek(fi, 0L, 0);
  320.         if (siz > 0 && (str = malloc(siz + 1))) {
  321.         fread(str, siz, 1, fi);
  322.         str[siz] = 0;
  323.         }
  324.         fclose(fi);
  325.     }
  326.     UnLock(CurrentDir(oldLock));
  327.     }
  328.     return(str);
  329. }
  330.  
  331. #endif
  332.  
  333. void
  334. SetDEnv(ename, econt)
  335. char *ename;
  336. char *econt;
  337. {
  338.     long envLock = Lock("env:", SHARED_LOCK);
  339.  
  340.     if (envLock) {
  341.     long oldLock = CurrentDir(envLock);
  342.     FILE *fi = fopen(ename, "w");
  343.  
  344.     if (fi) {
  345.         fwrite(econt, strlen(econt), 1, fi);
  346.         fclose(fi);
  347.     }
  348.     UnLock(CurrentDir(oldLock));
  349.     }
  350. }
  351.  
  352.  
  353. /*
  354.  *  GETFONT()
  355.  *
  356.  *  This function properly searches resident and disk fonts for the
  357.  *  font.
  358.  */
  359.  
  360. struct Library *DiskfontBase;
  361.  
  362. FONT *
  363. GetFont(name, size)
  364. char *name;
  365. short size;
  366. {
  367.     FONT *font1;
  368.     TA Ta;
  369.     short libwasopen = (DiskfontBase != (void *)NULL);
  370.  
  371.     Ta.ta_Name    = (UBYTE *)name;
  372.     Ta.ta_YSize = size;
  373.     Ta.ta_Style = 0;
  374.     Ta.ta_Flags = 0;
  375.  
  376.     font1 = OpenFont(&Ta);
  377.     if (font1 == NULL || font1->tf_YSize != Ta.ta_YSize) {
  378.     FONT *font2;
  379.  
  380.     if (libwasopen || (DiskfontBase = OpenLibrary("diskfont.library", 0))) {
  381.         if (font2 = OpenDiskFont(&Ta)) {
  382.         if (font1)
  383.             CloseFont(font1);
  384.         font1 = font2;
  385.         }
  386.         if (libwasopen == 0)
  387.         CloseLibrary(DiskfontBase);
  388.     }
  389.     }
  390.     return(font1);
  391. }
  392.  
  393. /*
  394.  *  DEAD.C
  395.  */
  396.  
  397. int
  398. DeadKeyConvert(msg,buf,bufsize,keymap)
  399. struct IntuiMessage *msg;
  400. UBYTE *buf;
  401. int bufsize;
  402. struct KeyMap *keymap;
  403. {
  404.     static struct InputEvent ievent = { NULL, IECLASS_RAWKEY };
  405.     if (msg->Class != RAWKEY)
  406.     return(-2);
  407.     ievent.ie_Code = msg->Code;
  408.     ievent.ie_Qualifier = msg->Qualifier;
  409.     ievent.ie_position.ie_addr = *((APTR *)msg->IAddress);
  410.     return(RawKeyConvert(&ievent,(char *)buf,bufsize,keymap));
  411. }
  412.  
  413. void *
  414. clrmem(ptr, bytes)
  415. void *ptr;
  416. size_t bytes;
  417. {
  418.     setmem(ptr, bytes, 0);
  419.     return(ptr);
  420. }
  421.  
  422.